package com.wust.tieshi;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.wust.R;
import com.wust.task.Task;
import com.wust.task.TaskDao;

public class AlarmAddActivity extends Activity{
	private Button datebtn;
	private Button timebtn;
	private EditText contenttext;
	private CheckBox isOpen;
	private Button savebtn;
	private Button canbtn;
	private boolean on_off=true;
	// 提醒日期
	private int mYear;
	private int mMonth;
	private int mDay;
		// 提醒时间
	private int mHour;
	private int mMinute;
	static final int DATE_DIALOG_ID = 0;
	static final int TIME_DIALOG_ID = 1;
	private TaskDao taskDao;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.alarm_add);
		datebtn=(Button)findViewById(R.id.alarm_add_date);
		timebtn=(Button)findViewById(R.id.alarm_add_time);
		contenttext=(EditText)findViewById(R.id.alarm_add_content);
		isOpen=(CheckBox)findViewById(R.id.isopen);
		savebtn=(Button)findViewById(R.id.alarm_save);
		canbtn=(Button)findViewById(R.id.alarm_cancel);
		final Calendar c = Calendar.getInstance();
		// 获得当前日期、时间
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
		datebtn.setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog(DATE_DIALOG_ID);
			}		
		});
		timebtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog(TIME_DIALOG_ID);
			}			
		});
		savebtn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (isOpen.isChecked()) {
					on_off = true;
				} else {
					on_off = false;
				}
				setAlarm(on_off);
				finish();
			}
			
		});
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		
		super.onPause();
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		// 显示日期对话框
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		// 显示时间对话框
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
					false);
		}
		return null;
	}
	// 时间选择对话框
		private TimePickerDialog.OnTimeSetListener mTimeSetListener = 
			new TimePickerDialog.OnTimeSetListener() {
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				mHour = hourOfDay;
				mMinute = minute;
				timebtn.setText(mHour + ":" + mMinute);
			}
		};
		// 日期选择对话框
		private DatePickerDialog.OnDateSetListener mDateSetListener = 
			new DatePickerDialog.OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				mYear = year;
				mMonth = monthOfYear+1;
				mDay = dayOfMonth;
				datebtn.setText(mYear + "/" + mMonth + "/" + mDay);
			}
		};
		// 设置通知提示
		private void setAlarm(boolean flag) {
			saveAlarm();
			final String BC_ACTION = "com.wust.TaskReceiver";
			List<Task> tasklist=taskDao.queryTasks();
			int id=tasklist.get(tasklist.size()-1).getId();
			
			// 获得AlarmManager实例
			final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
			// 实例化Intent
			Intent intent = new Intent();
			// 设置Intent action属性
			System.out.println("id"+id);
			intent.setAction(BC_ACTION);
			intent.putExtra("msg1", String.valueOf(id));
			
			// 实例化PendingIntent
			final PendingIntent pi = PendingIntent.getBroadcast(
					getApplicationContext(), 0, intent, 0);
			
			// 获得系统时间
			System.out.println("time1"+new Date().getTime());
			final long time1 = System.currentTimeMillis();
			System.out.println("time2:"+time1);
			Calendar c = Calendar.getInstance();
			c.set(mYear, mMonth-1, mDay, mHour, mMinute,0);
			
			System.out.println("year:"+mYear+"mMonth:"+mMonth+"day"+mDay+"hour"+mHour+"mINUTE"+mMinute);
			long time2 = c.getTimeInMillis();
			System.out.println(time2-time1);
			if (flag&&(time2-time1)>0){
				System.out.println("alarm set");
				am.set(AlarmManager.RTC_WAKEUP, time2, pi);
			}else{
				am.cancel(pi);
			}
		}
		private void saveAlarm()
		{
			 taskDao=new TaskDao(this);
			Task task=new Task();
			task.setAlarm(1);
			task.setDate1(datebtn.getText().toString());
			task.setTime1(timebtn.getText().toString());
			task.setCreated(contenttext.getText().toString());
			if(on_off==true)
				task.setOn_off(1);
			else
				task.setOn_off(0);
			taskDao.insertTask(task);
		}
}
